//
//  TextFiledView.m
//  Cxzc
//
//  Created by hwd on 17/8/7.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "TextFiledView.h"
@interface TextFiledView ()<UITextFieldDelegate>
{
    NSInteger limitNum;
    NSArray *numberKeyboardArr;
    NSArray *englishKeyboardArr;
    NSDictionary *limitDic;
}
@end
@implementation TextFiledView

-(instancetype)initWithFrame:(CGRect)frame PlaceHodel:(NSString *)placehodel Title:(NSString *)title{
    if (self = [super initWithFrame:frame]) {
        [self.layer setCornerRadius:5*screenW_6];
        self.layer.masksToBounds = YES;
        [self addSubview:[self CreatUIPlaceHodel:placehodel Title:title]];
    }
    return self;
}
-(UITextField *)CreatUIPlaceHodel:(NSString *)placehodel Title:(NSString *)title
{
    if (!_inputTf) {
        //Titlelab的大小控制
        NSInteger scrWidth = ([title isEqualToString:@"支付宝账号"] || [title isEqualToString:@"身份证号码"])?120*screenW_6:80*screenW_6;
        scrWidth = [title isEqualToString:@""]?0:scrWidth;
        _nameLab = [TomamaUI createLabel:CGRectMake(20*screenW_6,0,scrWidth,45*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:15*screenW_6] textColor:[UIColor colorWithHexString:@"#b5b5b5"] text:title];
        [self addSubview:_nameLab];
        
        _inputTf = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_nameLab.frame),0,SCREENW-scrWidth,45*screenW_6)];
        _inputTf.font = [UIFont systemFontOfSize:15*screenW_6];
        _inputTf.placeholder = placehodel;
        _inputTf.delegate = self;
        _inputTf.returnKeyType =  UIReturnKeyDefault;
        [_inputTf setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        _inputTf.textColor = [UIColor blackColor];
        [self addSubview:_inputTf];
        [_inputTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        //title为空隐藏titleLab
        if (![title isEqualToString:@""]) {
             [self addSubview:[ToolViewViewController addCellLineLab:CGRectMake(15*screenW_6, 44*screenW_6, self.frame.size.width-30*screenW_6, 1*screenW_6)]];
        }
        //键盘控制
        if ([self.numberKeyboardArr containsObject:placehodel]) {
            _inputTf.keyboardType = UIKeyboardTypeNumberPad;
        }else if ([self.englishKeyboardArr containsObject:placehodel]){
            _inputTf.keyboardType = UIKeyboardTypeASCIICapable;
        }
        //字数限制
        if ([self.limitDic.allKeys containsObject:placehodel]) {
            limitNum = [[self.limitDic objectForKey:placehodel] integerValue];
        }else{
            limitNum = 50;
        }
    }
    return _inputTf;
}

-(NSArray *)numberKeyboardArr//纯数字键盘
{
    if (!numberKeyboardArr) {
        numberKeyboardArr = @[@"请输入您的手机号码",
                              @"请输入您收到的验证码",
                              @"请输入手机号码",
                              @"请输入新手机号码",
                              @"请输入金额"];
    }
    return numberKeyboardArr;
}
-(NSArray *)englishKeyboardArr//先英文键盘
{
    if (!englishKeyboardArr) {
        englishKeyboardArr = @[];
    }
    return englishKeyboardArr;
}
-(NSDictionary *)limitDic//限制输入个数
{
    if (!limitDic) {
        limitDic = @{@"联系电话":@"16",
                     @"客户姓名":@"20",
                     @"地址":@"50",
                     @"联系人":@"20",
            };
    }
    return limitDic;
}
-(void)textFieldDidChange:(UITextField *)textField
{
    NSString *toBeString = textField.text;
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position) {
        
        if (toBeString.length > limitNum && limitNum!=0)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:limitNum];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:limitNum];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, limitNum)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }else
        {
//            NSString *keyType;
//            if ([textField.placeholder isEqualToString:@""]) {
//                keyType = ALPHANUM;
//            }else if ([textField.placeholder isEqualToString:@""]){
//                keyType = PHONEStr;
//            }
//            NSString *filtered;
//            if (keyType !=NULL) {
//                NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:keyType] invertedSet];
//                filtered = [[textField.text componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
//            }else
//            {
//                filtered = textField.text;
//            }
            if ([_inputTf.placeholder isEqualToString:@"请输入昵称"]||[_inputTf.placeholder isEqualToString:@"请输入姓名"]) {

                NSMutableString *testString = [NSMutableString stringWithString:toBeString];
                NSInteger alength = [testString length];
                for (int i = 0; i<alength; i++) {
                    char commitChar = [testString characterAtIndex:i];
                    NSString *temp = [testString substringWithRange:NSMakeRange(i,1)];
                    
                    unichar c = [testString characterAtIndex:i];
                    NSRange range = NSMakeRange(i, 1);
                    const char *u8Temp = [temp UTF8String];
                    if (3==strlen(u8Temp)){//字符串中含有中文
                    }else if((commitChar>64)&&(commitChar<91)){//字符串中含有大写英文字母
                    }else if((commitChar>96)&&(commitChar<123)){//字符串中含有小写英文字母
                    }else if((commitChar>47)&&(commitChar<58)){//字符串中含有数字
                    }else{
                        NSLog(@"字符串中含有非法字符11==%hu",c);
                        if (c == '-'  || c == '_') //此处可以是任何字符
                        {
                            
                        }else
                        {
                            NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, testString.length-1)];
                            textField.text = [toBeString substringWithRange:rangeRange];
                        }
                    }
                }
            }
            if (self.tfBlock) {
                self.tfBlock(textField.text);
            }
            _inputTf.text = textField.text;
        }
    }
}
+(NSString *) stringDeleteString:(NSString *)str
{
    NSMutableString *str1 = [NSMutableString stringWithString:str];
    for (int i = 0; i < str1.length; i++)
    {
        unichar c = [str1 characterAtIndex:i];
        NSRange range = NSMakeRange(i, 1);
        if (c == '-'  || c == '_') //此处可以是任何字符
        {
           
        }else
        {
            [str1 deleteCharactersInRange:range];
            --i;
        }
    }
    NSString *newstr = [NSString stringWithString:str1];
    
    return newstr;
}
@end
